<<<<<<< HEAD
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Oauth extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		if(!isset($_SESSION)){
		    session_start();
		}
		exit();
	}
	
	function index(){
		
	}
	
	

	
	//QQ登陆
	public function qq(){
		$this->load->library('/Oauth/qq2.0/oauth/qq_login');
		$cb = $this->session->userdata('callback');
		if ($_SERVER["HTTPS"]=="on"){
			$cb = str_replace("http://", "https://", $cb);
		}
		$http_referer = $_SERVER["HTTP_REFERER"];
		if(strpos($http_referer, "/login") === false){
			$this->session->set_userdata('url_referer',$http_referer);
		}
		
		$this->qq_login->qq($_SESSION["appid"], $_SESSION["scope"], $cb);
	}
	
	//QQ回调
	public function qqlogin(){
		$this->load->library('/Oauth/qq2.0/oauth/qq_callback');
		//QQ登录成功后的回调地址,主要保存access token
		$res = $this->qq_callback->qq();
		if($res !== true){
			print_r('回调 error');die;
		}
		//获取用户标示id
		$this->qq_callback->get_openid();
		//获取用户信息
		$userInfo = $this->qq_callback->get_user_info();
		$map['openid'] = text($_SESSION['openid']);//唯一ID
		$map['site'] = 'qq';
		$this->appCk($map,$userInfo->nickname);
	}
	
	private function appCk($map,$nickname){
		$this->load->model('Oauth_model', 'oauth');
		$vo = $this->oauth->get_one($map);
		
		if(is_array($vo) && count($vo)>0){//以前登陆过
			//该登陆方式已关闭？
			if($vo["login_enable"] != 1){
				$this->js->alert("该登陆方式已被用户禁用，不允许登陆！");
				$this->js->go("/login.html");
				return;
			}
			
			$data['id'] = $vo['id'];
			$data['logintimes'] = $vo['logintimes'];
			if($vo['is_bind']!=1){
				$data['is_bind'] = 1;
			}
			
			$this->logapp($data);
			$this->applogin($vo['bind_uid']);
			
			$url_referer = $_SESSION['url_referer'];
			if(!empty($url_referer)){
				$this->session->set_userdata('url_referer',null);
				$this->js->go($url_referer);
				return;
				
			}else{
				$this->js->go("/user/center/");
				return;
			}
		}elseif($nickname){
			$map['nickname'] = text($nickname);
			$map = $this->addapp($map);
			if(isset($map["bind_uid"])){
				$this->applogin($map['bind_uid']);
				$url_referer = $_SESSION['url_referer'];
				if(!empty($url_referer)){
					$this->session->set_userdata('url_referer',null);
					$this->js->go($url_referer);
					return;
				}else{
					$this->js->go("/user/center/");
					return;
				}
			}
		}
	}
	
	private function logapp($data){
		$save['id'] = $data['id'];
		$save['logintimes'] = ($data['logintimes']+1);
		$save['logintime'] = time();
		if(isset($data["is_bind"]))$save['is_bind']=$data["is_bind"];
		$this->load->model('Oauth_model', 'oauth');
		$this->oauth->update_oauth($save);
	}
	
	private function addapp($data){
		$this->load->model('Oauth_model', 'oauth');
		$this->load->model('User_model', 'user');
		$map=array();
		//会员表要保存的信息
		$count = 0;
		while(true){
			$remember['username'] = "@{$data['site']}_".$data['nickname'].sprintf("%05d",rand(1,99999));
			$map['username']  = $remember['user_name'];
			$member = $this->user->get_one($map);
			$memberCount = count($member);
			if($memberCount == 0){
				break;
			}
			++$count;
			if($count == 10){
				return $map;
			}
		}
		$remember['password'] = md5(md5($data['openid'].$data['site'].time()));
		$remember['add_time'] = time();
		$remember['addip'] = $this->input->ip_address();
		$remember['type_id'] = 2;
		
		$map['bind_uid'] = $this->user->add($remember);
		//会员表要保存的信息
		
		if($map['bind_uid']>0){
			$map['is_bind'] = 1;
			$map['nickname'] = $data['nickname'];
			$map['openid'] = $data['openid'];
			$map['site'] = $data['site'];
			$map['logintimes'] = 1;
			$map['addtime'] = time();
			$map['logintime'] = time();
			$newid = $this->oauth->add($map);
			$_SESSION['temp_bind_appid']=$map['bind_uid'];
			$_SESSION['temp_bind_oauth_id'] = $newid;
		}else{
			$newid = false;
		}
		return $map;
	}
	
	
	//新浪登陆
	public function sina(){
		define( "WB_AKEY" , '741018560' );
		define( "WB_SKEY" , '442bc9a0acdc9f3fcede3e151b195b80' );
		define( "WB_CALLBACK_URL" , "http://demo.yunchsoft.com/oauth/sinalogin");
		
		$this->load->library('/Oauth/sina/SaeTOAuthV2');
		
		//构造快捷登录接口
		$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );
		$code_url = $o->getAuthorizeURL( WB_CALLBACK_URL );
		$http_referer = $_SERVER["HTTP_REFERER"];
		if(strpos($http_referer, "/login") === false){
			$this->session->set_userdata('url_referer',$http_referer);
		}
		$this->js->go($code_url);
		return;
	}
	
	public function sinalogin(){
		define( "WB_AKEY" , '741018560' );
		define( "WB_SKEY" , '442bc9a0acdc9f3fcede3e151b195b80' );
		define( "WB_CALLBACK_URL" , "http://demo.yunchsoft.com/oauth/sinalogin");
		
		//$this->load->library('/Oauth/sina/saetv2.ex.class.php');
		$this->load->library('/Oauth/sina/SaeTOAuthV2');
		
		$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );
		
		if (isset($_REQUEST['code'])) {
			$keys = array();
			$keys['code'] = $_REQUEST['code'];
			$keys['redirect_uri'] = WB_CALLBACK_URL;
			try {
				$token = $o->getAccessToken( 'code', $keys ) ;
			} catch (OAuthException $e) {
			
			}
		}
		
		if(isset($_REQUEST['error_code'])){
			if($_REQUEST['error_code'] == '21330')
				$this->js->go('/login');
				return;
		}
		
		if ($token) {
			$_SESSION['token'] = $token;
		}else{
			exit("Error happenned, please try again.");
		}
		
		$sinauser = $o->getUserShow(text($token['uid']));
		
		$map['openid'] = text($token['uid']);//唯一ID
		$map['site'] = 'sina';
		$this->appCk($map, $sinauser['name']);//nickname
	}
	
	
	
	private function applogin($uid){//模拟登陆
		$this->load->model('User_model', 'user');
		$vo = $this->user->get_by_uid($uid);
		
		$user = array(
			'user_id' => $uid,
			'username' => $vo['username'],
			'email' => $vo['email'],
			'login_time' => time()
		);
		$this->session->set_userdata('rsh_user',$user);
	}
	
}
=======
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Oauth extends CI_Controller {
    
	function __construct(){
		parent::__construct();
		if(!isset($_SESSION)){
		    session_start();
		}
		exit();
	}
	
	function index(){
		
	}
	
	

	
	//QQ登陆
	public function qq(){
		$this->load->library('/Oauth/qq2.0/oauth/qq_login');
		$cb = $this->session->userdata('callback');
		if ($_SERVER["HTTPS"]=="on"){
			$cb = str_replace("http://", "https://", $cb);
		}
		$http_referer = $_SERVER["HTTP_REFERER"];
		if(strpos($http_referer, "/login") === false){
			$this->session->set_userdata('url_referer',$http_referer);
		}
		
		$this->qq_login->qq($_SESSION["appid"], $_SESSION["scope"], $cb);
	}
	
	//QQ回调
	public function qqlogin(){
		$this->load->library('/Oauth/qq2.0/oauth/qq_callback');
		//QQ登录成功后的回调地址,主要保存access token
		$res = $this->qq_callback->qq();
		if($res !== true){
			print_r('回调 error');die;
		}
		//获取用户标示id
		$this->qq_callback->get_openid();
		//获取用户信息
		$userInfo = $this->qq_callback->get_user_info();
		$map['openid'] = text($_SESSION['openid']);//唯一ID
		$map['site'] = 'qq';
		$this->appCk($map,$userInfo->nickname);
	}
	
	private function appCk($map,$nickname){
		$this->load->model('Oauth_model', 'oauth');
		$vo = $this->oauth->get_one($map);
		
		if(is_array($vo) && count($vo)>0){//以前登陆过
			//该登陆方式已关闭？
			if($vo["login_enable"] != 1){
				$this->js->alert("该登陆方式已被用户禁用，不允许登陆！");
				$this->js->go("/login.html");
				return;
			}
			
			$data['id'] = $vo['id'];
			$data['logintimes'] = $vo['logintimes'];
			if($vo['is_bind']!=1){
				$data['is_bind'] = 1;
			}
			
			$this->logapp($data);
			$this->applogin($vo['bind_uid']);
			
			$url_referer = $_SESSION['url_referer'];
			if(!empty($url_referer)){
				$this->session->set_userdata('url_referer',null);
				$this->js->go($url_referer);
				return;
				
			}else{
				$this->js->go("/user/center/");
				return;
			}
		}elseif($nickname){
			$map['nickname'] = text($nickname);
			$map = $this->addapp($map);
			if(isset($map["bind_uid"])){
				$this->applogin($map['bind_uid']);
				$url_referer = $_SESSION['url_referer'];
				if(!empty($url_referer)){
					$this->session->set_userdata('url_referer',null);
					$this->js->go($url_referer);
					return;
				}else{
					$this->js->go("/user/center/");
					return;
				}
			}
		}
	}
	
	private function logapp($data){
		$save['id'] = $data['id'];
		$save['logintimes'] = ($data['logintimes']+1);
		$save['logintime'] = time();
		if(isset($data["is_bind"]))$save['is_bind']=$data["is_bind"];
		$this->load->model('Oauth_model', 'oauth');
		$this->oauth->update_oauth($save);
	}
	
	private function addapp($data){
		$this->load->model('Oauth_model', 'oauth');
		$this->load->model('User_model', 'user');
		$map=array();
		//会员表要保存的信息
		$count = 0;
		while(true){
			$remember['username'] = "@{$data['site']}_".$data['nickname'].sprintf("%05d",rand(1,99999));
			$map['username']  = $remember['user_name'];
			$member = $this->user->get_one($map);
			$memberCount = count($member);
			if($memberCount == 0){
				break;
			}
			++$count;
			if($count == 10){
				return $map;
			}
		}
		$remember['password'] = md5(md5($data['openid'].$data['site'].time()));
		$remember['add_time'] = time();
		$remember['addip'] = $this->input->ip_address();
		$remember['type_id'] = 2;
		
		$map['bind_uid'] = $this->user->add($remember);
		//会员表要保存的信息
		
		if($map['bind_uid']>0){
			$map['is_bind'] = 1;
			$map['nickname'] = $data['nickname'];
			$map['openid'] = $data['openid'];
			$map['site'] = $data['site'];
			$map['logintimes'] = 1;
			$map['addtime'] = time();
			$map['logintime'] = time();
			$newid = $this->oauth->add($map);
			$_SESSION['temp_bind_appid']=$map['bind_uid'];
			$_SESSION['temp_bind_oauth_id'] = $newid;
		}else{
			$newid = false;
		}
		return $map;
	}
	
	
	//新浪登陆
	public function sina(){
		define( "WB_AKEY" , '741018560' );
		define( "WB_SKEY" , '442bc9a0acdc9f3fcede3e151b195b80' );
		define( "WB_CALLBACK_URL" , "http://demo.yunchsoft.com/oauth/sinalogin");
		
		$this->load->library('/Oauth/sina/SaeTOAuthV2');
		
		//构造快捷登录接口
		$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );
		$code_url = $o->getAuthorizeURL( WB_CALLBACK_URL );
		$http_referer = $_SERVER["HTTP_REFERER"];
		if(strpos($http_referer, "/login") === false){
			$this->session->set_userdata('url_referer',$http_referer);
		}
		$this->js->go($code_url);
		return;
	}
	
	public function sinalogin(){
		define( "WB_AKEY" , '741018560' );
		define( "WB_SKEY" , '442bc9a0acdc9f3fcede3e151b195b80' );
		define( "WB_CALLBACK_URL" , "http://demo.yunchsoft.com/oauth/sinalogin");
		
		//$this->load->library('/Oauth/sina/saetv2.ex.class.php');
		$this->load->library('/Oauth/sina/SaeTOAuthV2');
		
		$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );
		
		if (isset($_REQUEST['code'])) {
			$keys = array();
			$keys['code'] = $_REQUEST['code'];
			$keys['redirect_uri'] = WB_CALLBACK_URL;
			try {
				$token = $o->getAccessToken( 'code', $keys ) ;
			} catch (OAuthException $e) {
			
			}
		}
		
		if(isset($_REQUEST['error_code'])){
			if($_REQUEST['error_code'] == '21330')
				$this->js->go('/login');
				return;
		}
		
		if ($token) {
			$_SESSION['token'] = $token;
		}else{
			exit("Error happenned, please try again.");
		}
		
		$sinauser = $o->getUserShow(text($token['uid']));
		
		$map['openid'] = text($token['uid']);//唯一ID
		$map['site'] = 'sina';
		$this->appCk($map, $sinauser['name']);//nickname
	}
	
	
	
	private function applogin($uid){//模拟登陆
		$this->load->model('User_model', 'user');
		$vo = $this->user->get_by_uid($uid);
		
		$user = array(
			'user_id' => $uid,
			'username' => $vo['username'],
			'email' => $vo['email'],
			'login_time' => time()
		);
		$this->session->set_userdata('rsh_user',$user);
	}
	
}
>>>>>>> d47623b610d79a4eeb489500830c00d70933f414
